/*
 * common.cc
 *
 *  Created on: Mar 15, 2012
 *      Author: yexijiang
 */

#include "common.h"

namespace event {

using namespace std;

//string TimeToString(const time_t &timestamp)
//{
//	struct tm *time = gmtime(&timestamp);
//	char out_buf[128];
//	strftime(out_buf, 128, "%Y-%m-%d %Z %X", time);
//	string str = out_buf;
//	return str;
//}

int GetCPUCount() {
//	FILE * fp;
//	char res[128];
//	fp = popen("/bin/cat /proc/cpuinfo |grep -c '^processor'","r");
//	fread(res, 1, sizeof(res)-1, fp);
//	fclose(fp);
//
//	return atoi(res);

	cpu_set_t cs;
	CPU_ZERO(&cs);
	sched_getaffinity(0, sizeof(cs), &cs);
	int count = 0;
	for (int i = 0; i < 32; i++)
		if (CPU_ISSET(i, &cs))
			count++;
	return count;
}

//void ThreadSleep(long int sec, long int nanosec)
//{
//	struct timespec sleep_interval;
//	sleep_interval.tv_sec = sec;
//	sleep_interval.tv_nsec = nanosec;
//	struct timespec remain_sleep_interval;
//	nanosleep(&sleep_interval, &remain_sleep_interval);
//}
//
//void StringToFloat(const std::string &str, float &value)
//{
//	sscanf(str.c_str(), "%f", &value);
//}

void Split(const std::string &s, char c, std::vector<std::string> & v)
{
	int i = 0;
	int j = s.find(c);
	while(j >= 0)
	{
		v.push_back(s.substr(i, j - i));
		i = ++j;
		j = s.find(c, j);
		if(j < 0)
			v.push_back(s.substr(i, s.length()));
	}
}
};


